import { AppAisle } from "@lib/alen-helper/node/appAisle";
import { RunTime } from "../../runtime";

type Handlers = AppAisle.CmdHandlers<Runtime.Controller.Tray.BaseCmdData>;

export class TrayCmdHandlers implements Handlers {
    constructor(
        public readonly options: {},
        public readonly runTime: RunTime,
    ) {
        this.runTime.sendEvent(
            this.runTime.trayController, 'click', data => this.runTime.getApp(data.item.appid), 'tray_event_click',
            data => ({ appid: data.item.appid, id: data.item.id }),
        );
        this.runTime.sendEvent(
            this.runTime.trayController, 'double-click', data => this.runTime.getApp(data.item.appid), 'tray_event_double_click',
            data => ({ appid: data.item.appid, id: data.item.id }),
        );
    }

    private _getApp(aisle: AppAisle<AppAisle.Cmds>) {
        const app = this.runTime.getApp(aisle);
        if (!app || !app.id) throw new Error('找不到APP');
        return app;
    }
    //============
    tray_build: Handlers['tray_build'] = (data, aisle) => {
        const app = this._getApp(aisle);
        return this.runTime.trayController.start(app.id!, data.data.param.options);
    }

    tray_setIcon: Handlers['tray_setIcon'] = (data, aisle) => {
        const app = this._getApp(aisle);
        return this.runTime.trayController.setIcon(app.id!, data.data.param.id, data.data.param.icon)
    }

    tray_setToolTip: Handlers['tray_setToolTip'] = (data, aisle) => {
        const app = this._getApp(aisle);
        return this.runTime.trayController.setToolTip(app.id!, data.data.param.id, data.data.param.toolTip)
    }

}